﻿<?php

/**
 *
 *
 */
class TechnoDK_Mail extends TechnoDK_Object
{
	/**
	 *
	 *
	 *
	 */
	private $_message;
	
	/**
	 *
	 *
	 *
	 */
	private $_to;
	
	/**
	 *
	 *
	 *
	 */
	private $_from;
	
	/**
	 *
	 *
	 *
	 */
	private $_subject;
	
	/**
	 *
	 *
	 *
	 */
	private $_headers;
	
	/**
	 *
	 *
	 *
	 */
	private $_crlf = "\r\n";
	
	/**
	 *
	 *
	 *
	 */
	private $_isHtml;
	
	/**
	 *
	 *
	 *
	 */
	private $_separator = ", ";
	
	/**
	 *
	 *
	 *
	 */
	public function __construct($info = null)
	{
		if(has_value($info) && is_array($info)){
			if(isset($info['headers'])){
				$this->addHeaders($info['headers']);
			}
			if(isset($info['to'])){
				$this->setRecipient($info['to']);
			}
			if(isset($info['from'])){
				$this->setSender($info['from']);
			}
			if(isset($info['subject'])){
				$this->setSubject($info['subject']);
			}
			if(isset($info['message'])){
				$isHtml = isset($info['isHtml']) ? $info['isHtml'] : false;
				$this->setMessage($info['message'], $isHtml);
			}
		} else {
			$this->_html = false;
		}
	}
	
	/**
	 *
	 *
	 *
	 */
	public function setSender($from)
	{
		$from = trim($from);
		$this->_headers	 	= isset($this->_headers) ? $this->_headers : array();
		$this->_headers[]	= "From: {$from}";
		$this->_from 	 	= $from;
	}
	
	/**
	 *
	 *
	 *
	 */
	public function setRecipient($to)
	{
		$this->_headers	= isset($this->_headers) ? $this->_headers : array();
		if(is_array($to)){
			$to_tmp = array();
			foreach($to as $name => $email){
				$to_tmp[] = is_string($name) ? "{$name} <{$email}>" : $email;
			}
			$this->_to = trim(implode($this->_separator,$to_tmp));
			$this->_headers[] = "To: " . $this->_to;
		} else if(is_string($to)){
			$this->_to = trim($to);
			$this->_headers[] = "To: " . $this->_to;
		} else {
			throw new Exception("Invalid argument");
		}
	}
	
	/**
	 *
	 *
	 *
	 */
	public function isValidRecipient()
	{
		if(is_array($this->_to)){
			foreach($this->_to as $email){
				
			}
		} else if(is_string($this->_to)){
			
		}
		return false;
	}
	
	public function isValidSender()
	{
		preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]/",$this->_from,$match);
		return has_value($match);
	}
	
	public function hasErrors()
	{
	
	}
	
	public function isHtml()
	{
		return $this->_isHtml;
	}
	
	public function getErrors()
	{
		
	}
	
	public function getRecipient()
	{
		return explode($this->_separator,$this->_to);
	}
	
	/**
	 *
	 *
	 *
	 */
	public function setMessage($message, $isHtml = false)
	{
		if($isHtml){
			$this->_headers 	 = isset($this->_headers) ? $this->_headers : array();
			$this->_headers[] = 'MIME-Version: 1.0';
			$this->_headers[] = 'Content-type: text/html; charset=iso-8859-1';
			$this->_isHtml	 = true;
			$this->_message = $message;
		} else {
			$this->_message = strip_tags($message);
		}
	}
	
	/**
	 *
	 *
	 */
	public function __toString()
	{
		$mail_content  = $this->getHeaders() . "\n";
		$mail_content .= $this->_subject . "\n\n";
		$mail_content .= $this->_message;
		return $mail_content;
	}
	
	/**
	 *
	 *
	 *
	 */
	public function attachFile($file)
	{
	
	}
	
	/**
	 *
	 *
	 *
	 */
	public function getMessage()
	{
		return $this->_message;
	}
	
	/**
	 *
	 *
	 *
	 */
	public function setSubject($subject)
	{
		$this->_subject = $subject;
	}
	
	/**
	 *
	 *
	 *
	 */
	public function getSubject()
	{
		return $this->_subject;
	}
	
	/**
	 *
	 *
	 *
	 */
	public function addHeaders($header)
	{
		$this->_headers = isset($this->_headers) ? $this->_headers : array();
		if(is_array($header)){
			foreach($header as $title => $value){
				$this->_headers[] = "{$title}: {$value}";
			}
		} else if(is_string($header)){
			$this->_headers[] = $header;
		} else {
			throw new Exception("Invalid argument");
		}
	}
	
	/**
	 *
	 *
	 *
	 */
	public function setBcc($bcc)
	{
		$this->_headers = isset($this->_headers) ? $this->_headers : array();
		if(is_array($bcc)){
			$bcc_tmp = array();
			foreach($bcc as $name => $email){
				$bcc_tmp[] = is_string($name) ? "{$name} <{$email}>" : $email;
			}
			$this->_headers[] = "Bcc: " . trim(implode($this->_separator,$bcc_tmp));
		} else if(is_string($bcc)){
			$this->_headers[] = "Bcc: " . trim($bcc);
		} else {
			throw new Exception("Invalid argument");
		}
	}
	
	/**
	 *
	 *
	 *
	 */
	public function setCc($cc)
	{
		$this->_headers = isset($this->_headers) ? $this->_headers : array();
		if(is_array($cc)){
			$cc_tmp = array();
			foreach($cc as $name => $email){
				$cc_tmp[] = is_string($name) ? "{$name} <{$email}>" : $email;
			}
			$this->_headers[] = "Cc: " . trim(implode($this->_separator,$cc_tmp));
		} else if(is_string($cc)){
			$this->_headers[] = "Cc: " . trim($cc);
		} else {
			throw new Exception("Invalid argument");
		}
	}
	
	/**
	 *
	 *
	 *
	 */
	public function getHeaders()
	{
		return implode($this->_crlf,$this->_headers);
	}
	
	/**
	 *
	 *
	 *
	 */
	public function send()
	{
		return mail($this->_to,$this->_subject,$this->_message,$this->getHeaders());
	}
	


}


?>